/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cci;

import cdp.Cliente;
import cdp.Compra;
import cgt.AplCadastrarCliente;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Rodrigo
 */
public class CtrlPrincipal extends HttpServlet {
    
    String janela;
    String opcao;
    
    String login;
    String senha;
    
    AplCadastrarCliente aplCliente = AplCadastrarCliente.getInstance();
    
    private void setParametros(HttpServletRequest request, HttpServletResponse response)
    {
        janela = request.getParameter("janela");
        
        //if (janela.equals("Menu")) {
           // opcao = "logoff";
        //}
        //else{
            opcao = request.getParameter("opcao");
        //}
    }
    
    public void criaCarrinho(HttpServletRequest request){
        List<Compra> carrinho = new ArrayList<Compra>();
        Double total = new Double(0.0);
        //Lista de compras
        request.getSession().setAttribute("carrinho", carrinho);
        //Objeto para guardar o total das compras
        request.getSession().setAttribute("total", total);
    }
    
    public void criaMensagens(HttpServletRequest request){
        List<Compra> carrinho = new ArrayList<Compra>();
        request.getSession().setAttribute("carrinho", carrinho);
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CtrlPrincipal</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CtrlPrincipal at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("teste");
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        HttpSession session;
        Cliente cliente;
        String url = "/";
        String alerta = "";
        RequestDispatcher dispatcher = null;
        
        setParametros(request, response);
        
        if(janela.equals("Login")){
            
            
            if (opcao.equals("autenticacao")) {
                
                url = "index.jsp";
                
                login = request.getParameter("login");
                senha = request.getParameter("senha");
                
                if(login.equals("admin")){
                    if (senha.equals("123")) {
                        session = request.getSession();
                        session.setAttribute("modo", "administrador");  
                    }
                    else{
                        url = "Login.jsp";
                        
                        alerta = "Senha incorreta!";
                        request.setAttribute("alerta", alerta);
                    }
                    
                    dispatcher = request.getRequestDispatcher(url);
                    dispatcher.forward(request, response);
                }
                else{
                    cliente = aplCliente.autenticarSenha(login, senha);

                    if (cliente != null) {
                        session = request.getSession();
                        session.setAttribute("modo", "usuario");
                        session.setAttribute("cliente", cliente);
                        
                        //Criando listas de objetos na session
                        criaCarrinho(request);
                        //criaMensagens(request);  
                    }
                    else{
                        url = "Login.jsp";
                        
                        alerta = "Usuário não cadastrado ou senha incorreta!";
                        request.setAttribute("alerta", alerta);
                    }
                    
                    dispatcher = request.getRequestDispatcher(url);
                    dispatcher.forward(request, response);
                }  
            }
            
            if (opcao.equals("cadastro")) {
                url = "cih/PagFormCliente.jsp";
                dispatcher = request.getRequestDispatcher(url);
                dispatcher.forward(request, response); 
            }
        }
        
        /*Logoff*/
        if (janela.equals("Menu")) {
            if (opcao.equals("logoff")) {
                session = request.getSession();
                session.invalidate();
                response.sendRedirect("Login.jsp"); 
            }
            
        }
        
        //processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
